package com.example.poemtest.mine;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.poemtest.R;
import com.example.poemtest.entity.DynastryPoetryBean;
import com.example.poemtest.entity.PoetryInfoBean;
import com.example.poemtest.index.entity.Poetry;

import java.util.List;

/**
 * created by JHT on 2024/4/2
 * 我的诗单适配器
 */
public class MyPoetryCreationAdapter extends BaseAdapter {

    private Context mContext;
    private List<PoetryInfoBean.PoetryInfo> myCreateList;
    private int layoutResId;

    public MyPoetryCreationAdapter(Context mContext, List<PoetryInfoBean.PoetryInfo> myCreateList, int layoutResId) {
        this.mContext = mContext;
        this.myCreateList = myCreateList;
        this.layoutResId = layoutResId;
    }


    @Override
    public int getCount() {
        if (myCreateList != null) {
            return myCreateList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (myCreateList != null) {
            return myCreateList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            //加载item的布局控件，生成一个view对象，再给convertView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId, null);
            //获取item中的控件对象
            viewHolder = new ViewHolder();
            viewHolder.poetryContent = convertView.findViewById(R.id.poetry_content);
            viewHolder.poetryTitle = convertView.findViewById(R.id.poetry_title);
            viewHolder.poetryAuther= convertView.findViewById(R.id.poetry_auther);
//            viewHolder.poetryDate = convertView.findViewById(R.id.my_poetry_data);
            //将viewHolder对象同convertView关联起来
            convertView.setTag(viewHolder);
        } else {
            //重复利用convertView对象
            //获取到跟当前的convertView绑定在一起的viewHolder对象
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.poetryContent.setText(myCreateList.get(position).getPoetryContent());
        viewHolder.poetryTitle.setText(myCreateList.get(position).getPoetryTitle());
        viewHolder.poetryAuther.setText(myCreateList.get(position).getPoetryAuthor());
//        viewHolder.poetryDate.setText(myPoetryList.get(position).getCreatDate().toString());
        return convertView;
    }

    private static class ViewHolder {
        private TextView poetryContent, poetryTitle, poetryAuther;
    }

}
